package cn.nfwx.tv.widget;

import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Calendar;

import cn.nfwx.tv.R;

/**
 * Created by zhouzechao on 2017/11/17.
 */

public class TitleBar extends RelativeLayout {

    TextView time, power, tv_qpop_wifi;
    BatteryView powerview;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_title, this, true);
        time = findViewById(R.id.tv_title_time);
        powerview = findViewById(R.id.bv_title_power);
        power = findViewById(R.id.tv_title_power);
        tv_qpop_wifi = findViewById(R.id.tv_qpop_wifi);

        String timeStr = updateTime();
        time.setText(timeStr);
    }

    public void showQpopWIFI() {
        tv_qpop_wifi.setVisibility(VISIBLE);
    }

    public void hideQpopWIFI() {
        tv_qpop_wifi.setVisibility(GONE);
    }

    public void onReceive(Intent intent) {
        final String action = intent.getAction();
        if (Intent.ACTION_BATTERY_CHANGED.equalsIgnoreCase(action)) {

            int current = intent.getExtras().getInt(BatteryManager.EXTRA_LEVEL);
            int total = intent.getExtras().getInt(BatteryManager.EXTRA_SCALE);// 获得总电量
            int percent = current * 100 / total;
            power.setText(percent + "%");

            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            switch (status) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    // 正在充电
                    powerview.setBolt(true);
                    powerview.setPower(percent);
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    // 没有充电
                    powerview.setBolt(false);
                    powerview.setPower(percent);
                    break;
                default:
                    break;
            }
        }
    }

    public void update() {
        String timeStr = updateTime();//每一分钟更新时间
        time.setText(timeStr);
    }

    boolean is24hFormart = true;

    private String updateTime() {
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        String timeStr = "PM";


        if (hour < 12) {
            timeStr = "AM";
        }

        if (!is24hFormart && hour >= 12) {
            hour = hour - 12;
        }

        String time = "";
        if (hour >= 10) {
            time += Integer.toString(hour);
        } else {
            time += "0" + Integer.toString(hour);
        }
        time += ":";

        if (minute >= 10) {
            time += Integer.toString(minute);
        } else {
            time += "0" + Integer.toString(minute);
        }

        return time + (is24hFormart ? "" : " " + timeStr);
    }
}
